import pandas as pd
import requests
import threading

def fetch_weather_data(url_template, year, month, day, hour):
    # 生成完整的 URL
    url = url_template.format(year=year, month=month, day=day, hour=hour)
    
    results = []  # 用于存储所有爬取的数据
    
    try:
        # 发送请求获取HTML内容
        response = requests.get(url)
        response.raise_for_status()  # 如果响应状态码不是200，将引发HTTPError异常
        html_content = response.text
        # 使用pandas读取HTML表格数据
        dfs = pd.read_html(html_content)
        # 确保我们获取了正确的表格
        if len(dfs) > 0:
            # 假设第一个表格是我们需要的，将其转换为DataFrame
            df = dfs[0]
            # 根据URL生成Excel文件名
            excel_name = "weather_data_" + url.split("/")[-5] + "_" + url.split("/")[-4] + "_" + url.split("/")[-3] + "_" + url.split("/")[-2]
            # 将DataFrame保存到Excel文件中
            with pd.ExcelWriter(excel_name + ".xlsx") as writer:
                df.to_excel(writer, index=False, sheet_name=excel_name)
            print(f"数据已保存到Excel文件中（{url}）。")
            results.append(df)  # 将爬取的数据添加到结果列表中
        else:
            print(f"未能解析表格数据（{url}）。")
    except requests.HTTPError as http_err:
        print(f'HTTP error occurred: {http_err}（{url}）')
    except Exception as err:
        print(f'An error occurred: {err}（{url}）')
    
    # 将所有爬取的数据合并为一个DataFrame
    if results:
        combined_df = pd.concat(results, ignore_index=True)
        return combined_df
    else:
        print("没有可用的数据.")
        return None

def threaded_fetch_weather_data(url_template, urls):
    # 创建线程列表
    threads = []
    # 用于存储所有线程的结果
    thread_results = []
    
    for url_params in urls:
        year, month, day, hour = url_params  # 解包 URL 参数
        t = threading.Thread(target=fetch_weather_data, args=(url_template, year, month, day, hour))
        threads.append(t)
        t.start()
    
    for t in threads:
        t.join()
    
    return thread_results

# 定义URL模板
url_template = "https://www.wx-now.com/Archival/ZGSZ/{year}/{month}/{day}/{hour}/00"
# 生成URL列表

urls = [
    (year, month, day, hour)
    for year in range(2024, 2025)  # 从2024年到2024年
    for month in range(4, 6)        # 4月到5月
    for day in range(1, 31)         # 1日到31日
    for hour in range(0, 24)        # 0点到23点
]

# 使用多线程爬取数据
combined_df = threaded_fetch_weather_data(url_template, urls)
